package com.guang.lock.condition;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Description 使用ReentrantLock中的条件队列来演示
 *              但是Tom线程一直不工作。所以得让老板修改加班制度
 * @Author liguang
 * @Date 2022/10/20/19:13
 */
public class SyncronizedConditionProReplace2ReentrantLockTest {

    private final static Logger logger = LoggerFactory.getLogger(SyncronizedConditionProReplace2ReentrantLockTest.class);

    // lock对象
    static ReentrantLock lock = new ReentrantLock();

    // 创建显示队列
    static Condition condition = lock.newCondition();

    // 加班是否有假可调休
    static boolean haveVocation = false;

    public static void main(String[] args) {

        // tom是高级开发。无所不能，无所不会
        Thread tom = new Thread(() -> {
            workUp();
        }, "tom");
        tom.start();

        for (int i = 0; i < 10; i++) {
            new Thread(() -> {
                workUp();
            }, "normal").start();
        }


    }

    public static void workUp() {
        lock.lock();
        if (Thread.currentThread().getName().equals("tom")) {
            if (!haveVocation) {
                logger.info("当前加班没有调休，那么tom就不干，什么时候有假期，我什么时候干活");
                try {
                    condition.await();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            logger.info("Tom大佬开始写代码");
        } else {
            logger.info("Tom在休息的时候不干，我们普通程序员来干");
        }
        lock.unlock();
    }
}


